﻿using System;
using System.Net;
using Ext.Net;

namespace ERP.Modules
{
    public partial class MCK_Reason_TimeKeeper : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                GetData();
                btnEdit.Disable(true);
                btnDel.Disable(true);
                btnExport.Disable(true);
            }
        }

        protected void StoreMCK_Reason_TimeKeeper_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            GetData();
        }

        protected void btnAddNew_Click(object sender, DirectEventArgs e)
        {
            ClearTextBox();
            WindowsMCK_Reason_TimeKeeper.Show();
        }

        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            var sm = grMCK_Reason_TimeKeeper.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    ClearTextBox();
                    DataDetail(int.Parse(row.RecordID));
                    WindowsMCK_Reason_TimeKeeper.Show();
                    sm.UpdateSelection();
                }
        }

        protected void MCK_Reason_TimeKeeper_DblClick(object sender, DirectEventArgs e)
        {
            ClearTextBox();
            int Reason_TimeKeeper_ID = int.Parse(e.ExtraParams["Reason_TimeKeeper_ID"]);
            DataDetail(Reason_TimeKeeper_ID);
            WindowsMCK_Reason_TimeKeeper.Show();
        }

        protected void btnSavePopUp_Click(object sender, DirectEventArgs e)
        {
            if (txtReason_TimeKeeper_ID.Text == string.Empty)
            {
                try
                {
                    var obj = new ET.MCK_Reason_TimeKeeper_ET
                    {
                        Enroll_Number = "1",
                        Symbol_TimeKeeper_ID = 1,
                        Date = DateTime.Now,
                        User_Add = 1,
                        Date_Add = DateTime.Now,
                        Computer_Add = Dns.GetHostEntry(Request.ServerVariables["remote_addr"]).HostName + " IP: " + Dns.GetHostEntry(Dns.GetHostName()).AddressList.GetValue(0)
                    };
                    BLL.MCK_Reason_TimeKeeper_BLL.Insert_MCK_Reason_TimeKeeper(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Thêm mới thành công !");
                    GetData();
                    ClearTextBox();
                }
                catch (Exception ex)
                {
                    HTML.clsFormat.Alert_ERROR("Lỗi", ex.ToString());
                }
            }
            else
            {
                try
                {
                    var obj = new ET.MCK_Reason_TimeKeeper_ET
                    {
                        Reason_TimeKeeper_ID = int.Parse(txtReason_TimeKeeper_ID.Text),
                        //Reason_TimeKeeper_Alias = txtReason_TimeKeeper_Alias.Text,
                        //Reason_TimeKeeper_Name = txtReason_TimeKeeper_Name.Text,
                        //Reason_TimeKeeper_Day = float.Parse(txtReason_TimeKeeper_Day.Text),
                        //Reason_TimeKeeper_Note = txtReason_TimeKeeper_Note.Text
                    };
                    BLL.MCK_Reason_TimeKeeper_BLL.Update_MCK_Reason_TimeKeeper(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Sửa thành công !");
                    GetData();
                }
                catch (Exception ex)
                {
                    HTML.clsFormat.Alert_ERROR("Lỗi", ex.ToString());
                }
            }
        }

        protected void btnDel_Click(object sender, DirectEventArgs e)
        {
            var sm = grMCK_Reason_TimeKeeper.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    try
                    {
                        BLL.MCK_Reason_TimeKeeper_BLL.Delete_MCK_Reason_TimeKeeper(int.Parse(row.RecordID));
                    }
                    catch (Exception)
                    {
                        HTML.clsFormat.Alert_ERROR("Thông Báo", "Tồn tại tài liệu con, không xoá được vui lòng xoá tài liệu con trước !");
                    }
                }
            GetData();
        }
        #region Get
        private void GetData()
        {
            StoreMCK_Reason_TimeKeeper.DataSource = BLL.MCK_Reason_TimeKeeper_BLL.Select_MCK_Reason_TimeKeeper(string.Empty);
            StoreMCK_Reason_TimeKeeper.DataBind();
        }
        private void DataDetail(int id)
        {
            var dt = BLL.MCK_Reason_TimeKeeper_BLL.SelectRow_MCK_Reason_TimeKeeper(id);
            if (dt != null && dt.Rows.Count > 0)
            {
                //txtReason_TimeKeeper_ID.Text = dt.Rows[0]["Reason_TimeKeeper_ID"].ToString();
                //txtReason_TimeKeeper_Alias.Text = dt.Rows[0]["Reason_TimeKeeper_Alias"].ToString();
                //txtReason_TimeKeeper_Name.Text = dt.Rows[0]["Reason_TimeKeeper_Name"].ToString();
                //txtReason_TimeKeeper_Day.Text = dt.Rows[0]["Reason_TimeKeeper_Day"].ToString().Replace(".", ",");
                //txtReason_TimeKeeper_Note.Text = dt.Rows[0]["Reason_TimeKeeper_Note"].ToString();
            }
        }
        private void ClearTextBox()
        {
            FormPanelMCK_Reason_TimeKeeper.Reset();

            txtReason_TimeKeeper_ID.Text = string.Empty;
        }
        #endregion
    }
}